package com.example.idlefish.PublicModule

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Base64
import android.util.Log
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import com.example.idlefish.R
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import java.io.ByteArrayOutputStream
import kotlin.concurrent.thread

object ApplicationConst {

    // 虚拟机IP
    private const val Virtual_Machine_IP = "10.0.2.2"

    // 真机IP
    private const val Real_Machine_IP = "192.168.45.39"
    private const val Real_Machine_IP_ZHANGYU = "192.168.137.1"


    const val server_ip = Virtual_Machine_IP

    var goodPic1:Bitmap? = null



    enum class post_method(val value:Int){
        BAOYOU(1),
        ANJULI(2),
        YIKOUJIA(3),
        WUXUYOUJI(4);
    }
    //上传商品图片
    @JvmStatic
    fun uploadImage(bitmap: Bitmap, callback: Callback, fileName:String)
    {
        thread {
            val byteArray = bitmapToByteArray(bitmap)
            val requestBody = RequestBody.create("image/jpeg".toMediaTypeOrNull(), byteArray)
            val part = MultipartBody.Part.createFormData("image", fileName, requestBody)
            val client= OkHttpClient()
            val request = Request.Builder()
                .url("http://${server_ip}:8080/uploadImage")
                .header("Content-Type", "multipart/form-data")
                .post(MultipartBody.Builder().setType(MultipartBody.FORM).addPart(part).build())
                .build()
            client.newCall(request).enqueue(callback)
        }
    }

    @JvmStatic
    fun decodeBase64ToImage(context: Context, ImageString:String?):Bitmap?{
        if(ImageString == null)
            return  ResourcesCompat.getDrawable(context.resources, R.drawable.default_image, null)?.toBitmap()

        return try {
            val decodeBytes = Base64.decode(ImageString, Base64.DEFAULT)
            BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.size)
        } catch (e: Exception) {
            // 返回默认图片
            e.printStackTrace()
            ResourcesCompat.getDrawable(context.resources, R.drawable.default_image, null)?.toBitmap()
        }
    }
    @JvmStatic
    fun uploadAvatarImage(bitmap: Bitmap, callback: Callback, fileName:String)
    {
        thread {
            val byteArray = bitmapToByteArray(bitmap)
            val requestBody = RequestBody.create("image/jpeg".toMediaTypeOrNull(), byteArray)
            val part = MultipartBody.Part.createFormData("image", fileName, requestBody)
            val client= OkHttpClient()
            val request = Request.Builder()
                .url("http://${server_ip}:8080/user/uploadUserAvatar")
                .header("Content-Type", "multipart/form-data")
                .post(MultipartBody.Builder().setType(MultipartBody.FORM).addPart(part).build())
                .build()
            client.newCall(request).enqueue(callback)
        }
    }

    //将bitmap转为字节数组
    @JvmStatic
    fun bitmapToByteArray(bitmap: Bitmap):ByteArray{
        val outputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG,100, outputStream)
        return outputStream.toByteArray()
    }


    @JvmStatic
    fun NetRequestForGoodContext(goodId:Long, callback: Callback){
        val client = OkHttpClient()
        Log.i("为什么在这样我受不了了{}", UserConst.id.toString())
        val jsp = "http://${server_ip}:8080/queryGoodContext?goodId=${goodId}&userId=${UserConst.id}"
        val request = Request.Builder()
            .url(jsp).get().build()
        client.newCall(request).enqueue(callback)
    }


    //商品搜索信息
    @JvmStatic
    fun NetRequestForEaseGoodContext(goodId:Long, callback: Callback){
        val client = OkHttpClient()
        val jsp = "http://${server_ip}:8080/queryEaseGoodInfo?goodId=${goodId}"
        val request = Request.Builder()
            .url(jsp).get().build()
        client.newCall(request).enqueue(callback)
    }

    @JvmStatic
    fun getBitmapFromUri(context: Context, uri: Uri): Bitmap? {
        return try {
            context.contentResolver.openInputStream(uri)?.use { inputStream ->
                BitmapFactory.decodeStream(inputStream)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}